enum TTS_VAR {
//% block="0"
    0,
    //%block="1"
    1,
    //%block="2"
    2,
    //% block="3"
    3,
    //%block="4"
    4,
    //% block="5"
    5,
    //%block="6"
    6,
    //% block="7"
    7,
    //%block="8"
    8,
    //% block="9"
    9
}

enum TTS_RING {
    //% block="ring_1"
    ring_1,
    //%block="ring_2"
    ring_2,
    //%block="ring_3"
    ring_3,
    //% block="ring_4"
    ring_4,
    //%block="ring_5"
    ring_5,
    //% block="message_1"
    message_1,
    //%block="message_2"
    message_2,
    //% block="message_3"
    message_3,
    //%block="message_4"
    message_4,
    //% block="message_5"
    message_5,
    //%block="alert_1"
    alert_1,
    //% block="alert_2"
    alert_2,
    //%block="alert_3"
    alert_3,
    //% block="alert_4"
    alert_4,
    //%block="alert_5"
    alert_5
}

//% color="#0f6cd3" iconWidth=50 iconHeight=40
namespace SNR9816TTS {
    //% block="init tts module[PIN_RX]" blockType="command"
    //% PIN_RX.shadow="dropdown" PIN_RX.options="PIN_RX"
    export function ttsSetup(parameter: any, block: any) {
    let PIN_RX = parameter.PIN_RX.code
    if(Generator.board == 'arduino'){//如果是arduinouno板，生成如下代码
    Generator.addSetup("GTSerialSetup",`Serial.begin(115200);`);
    }else if(Generator.board == 'esp32'){//如果是掌控板，生成如下代码
    Generator.addInclude('SNR_9816_TTS', '#include <SNR_9816_TTS.h>');
    Generator.addSetup("Serial2Setup",`Serial2.begin(115200 ,-1,${PIN_RX});`);
    }else if(Generator.board == 'esp8266'){
    Generator.addInclude('utf8togb2312', '#include <utf8togb2312.h>');
    Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
    Generator.addInclude('tts', '#include <tts.h>\ntts tts;');
    Generator.addInclude("SoftwareSerial1", `SoftwareSerial ttsSerial(-1,${PIN_RX});`);
    Generator.addSetup("ttsSerial",`ttsSerial.begin(115200);`);
    Generator.addSetup("ttsSetup",`tts.begin(ttsSerial);`);
    }
    }

    //% block="语音合成 语调[tts_tone] 语速[tts_speed]音量[tts_volume][VAL]" blockType="command"
    //% tts_tone.shadow="dropdown" tts_tone.options="TTS_VAR" tts_tone.defl="TTS_VAR.5"
    //% tts_speed.shadow="dropdown" tts_speed.options="TTS_VAR" tts_speed.defl="TTS_VAR.5"
    //% tts_volume.shadow="dropdown" tts_volume.options="TTS_VAR" tts_volume.defl="TTS_VAR.5"
    //% VAL.shadow="string" VAL.defl=你好
    export function tts(parameter: any, block: any) {
    let tts_tone = parameter.tts_tone.code;
    let tts_speed = parameter.tts_speed.code;
    let tts_volume = parameter.tts_volume.code;
    let str = parameter.VAL.code;
    if(Generator.board == 'esp32'){//如果是掌控板，生成如下代码
    let new_str="\"[t"+tts_tone.toString()+"][s"+tts_speed.toString()+"][v"+tts_volume.toString()+"]\"+String("+str+")";
    Generator.addCode(`TTS(${new_str});`);
    }else if(Generator.board == 'esp8266'){
    let new_str="\"[t"+tts_tone.toString()+"][s"+tts_speed.toString()+"][v"+tts_volume.toString()+"]\"+String("+str+")";
    Generator.addCode(`tts.play(utf8togb2312(${new_str}));`);
    }
    }

    //% block="铃声播报 音量[tts_volume] 铃声[tts_ring]" blockType="command"
    //% tts_volume.shadow="dropdown" tts_volume.options="TTS_VAR" tts_volume.defl="TTS_VAR.5"
    //% tts_ring.shadow="dropdown" tts_ring.options="TTS_RING" tts_ring.defl="TTS_RING.ring_1"
    export function ttsring(parameter: any, block: any) {
    let tts_volume = parameter.tts_volume.code;
    let tts_ring = parameter.tts_ring.code;
    let new_str="\"[v"+tts_volume.toString()+"]，"+tts_ring+"\"";
    if(Generator.board == 'esp32'){//如果是掌控板，生成如下代码
    Generator.addCode(`TTS(${new_str});`);
    }else if(Generator.board == 'esp8266'){
    Generator.addCode(`tts.play(utf8togb2312(${new_str}));`);
    }
    }
}
